package com.ctgu.demo03.controller;

import com.ctgu.demo03.service.StocksService;
import com.ctgu.demo03.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName StocksController
 * @Author Alex2
 * @Date 2025/3/12 8:51
 **/
@RestController
@RequestMapping("/api/stocks")
public class StocksController {

    @Autowired
    private StocksService stocksService;

    /**
     * @Author: Alex
     * @Description:添加库存
     */
    // http://localhost:8080/api/stocks/insertStock?name=voucher&stock=100
    @GetMapping("insertStock")
    public ApiResult insertStock(@RequestParam("name") String name,@RequestParam("stock") Integer stock) {
        return stocksService.insertStock(name, stock);
    }

    /**
     * @Author: Alex
     * @Description: CAS实现乐观锁
     */
    // http://localhost:8080/api/stocks/fetchVoucher?id=1
    @GetMapping("fetchVoucher")
    public ApiResult fetchVoucher(@RequestParam("id") Integer id) {
        return stocksService.fetchVoucher(id);
    }
}
